import unittest
import requests
from api.RegLoginApi import RegLoginApi
from api.RechargeApi import RechargeApi
from common.read_html_util import html_util
from parameterized import parameterized
from common.resd_json import read_json


class TestRecharge(unittest.TestCase):

    def setUp(self) -> None:
        # 创建session对象
        self.ses = requests.session()
        # 实例化登录模块封装的类
        self.reg_api = RegLoginApi(self.ses)
        # 实例化充值模块封装的类
        self.recharge_api = RechargeApi(self.ses)

    def tearDown(self) -> None:
        # 关闭session对象
        self.ses.close()

    # 获取充值验证码测试用例
    @parameterized.expand(read_json("test03_data", "api1_data"))
    def test01_get_recharge_verify_code(self, r, expect_status_code):
        # 获取充值图片验证码
        # 断言
        resp = self.recharge_api.get_recharge_verify_code(r)
        self.assertEqual(expect_status_code, resp.status_code)

    # 充值测试用例
    @parameterized.expand(read_json("test03_data", "api2_data"))
    def test02_recharge(self, paymentType, amount, formStr, valicode, expect_status_code, expect_status,
                        expect_description):
        # 登录
        # 获取充值图片验证码
        # 充值
        # 断言
        self.reg_api.user_login("18373444511", "11111MM")
        self.recharge_api.get_recharge_verify_code(123)
        form_data = {"paymentType": paymentType,
                     "amount": amount,
                     "formStr": formStr,
                     "valicode": valicode}
        resp = self.recharge_api.recharge(form_data)
        self.assertEqual(expect_status_code, resp.status_code)
        self.assertEqual(expect_status, resp.json().get("status"))
        self.assertIn(expect_description, resp.json().get("description"))

    # 第三方充值测试用例
    def test03_third_recharge(self):
        # 登录
        # 获取充值图片验证码
        # 充值
        # 获取第三方充值接口的url
        # 获取第三方充值接口的请求体数据
        # 获取第三方接口返回的响应结果
        # 断言
        self.reg_api.user_login("18373444511", "11111MM")
        self.recharge_api.get_recharge_verify_code(123)
        form_data = {"paymentType": "chinapnrTrust",
                     "amount": "100000",
                     "formStr": "reForm",
                     "valicode": "8888"}
        data_cz = self.recharge_api.recharge(form_data)
        url = html_util(data_cz)[0]
        form_dict = html_util(data_cz)[1]
        resp = self.recharge_api.third_recharge(url, form_dict)
        self.assertEqual(200, resp.status_code)
        self.assertIn("OK", resp.text)
